package linter.linterjava.controller.user;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import linter.linterjava.POJO.DO.User;
import linter.linterjava.POJO.DTO.UserDTO;
import linter.linterjava.custom.ResultVO;
import linter.linterjava.service.UserQueryService;
import linter.linterjava.service.UserService;
import linter.linterjava.utils.JwtUtils;
import linter.linterjava.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 北煜
 */
@Slf4j
@RequestMapping("user")
@RestController
@Api(tags = "基础用户模块")
public class BaseUserController {

    private final UserQueryService userQueryService;
    @Autowired
    public BaseUserController(UserQueryService userQueryService) { this.userQueryService = userQueryService; }
    @GetMapping("/show/getUserByUsername/{username}")
    @ApiOperation("根据用户名查找用户")
    public ResultVO<UserDTO> getUserByUsername(@PathVariable String username) { return userQueryService.getUserByUsername(username); }

    @Resource
    UserService userService;

    @PostMapping("/login")
    @ApiOperation("登录")
    public ResultVO<?> login(@RequestBody User user){
        System.out.println(user);
        String username = user.getUserName();
        String pwd = user.getUserPassword();
        String result =userService.checkLogin(username,pwd);
        // 三种返回：1.用户不存在 2.密码错误 3.登录成功返回token
        if(result.length() > 20){
            return ResultVO.success("登录成功",result);
        }
        return ResultVO.error(result);
    }

    @PostMapping("/register")
    @ApiOperation("注册")
    public ResultVO<String> register(@RequestBody User user) {
        String username = user.getUserName();
        String pwd = user.getUserPassword();
        String phone = "17695718400";
        String email = "3411707878@qq.com";
        if (user.getUserPhone() !=null){ phone = user.getUserPhone(); }
        if (user.getUserEmail() !=null){ email = user.getUserEmail(); }
        UserDTO checkUser = userService.getUserByUsername(username);
        if (checkUser != null){ return ResultVO.error("用户名已存在"); }
        Long result = userService.register(username,pwd,phone,email);
        if(result == 3L){ return ResultVO.success("注册成功"); }
        else {
            if (result == 0L) { return ResultVO.error("注册失败,请重试"); }
            else if (result == -1L) {
                log.error("注册失败,数据库回滚错误，检查用户表");
                return ResultVO.error("注册失败,系统错误");
            }
        }
        return ResultVO.error("注册失败");
    }


    @DeleteMapping("/outLogin")
    @ApiOperation("退出登录")
    public ResultVO<?> outLogin() {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        System.out.println(username);
        if (username != null){
            if (RedisUtils.hasKey(username)){ RedisUtils.del(username); }
            return ResultVO.success("成功退出登录");
        }
        return ResultVO.error("退出登录异常");
    }

    @DeleteMapping("/closeUser")
    @ApiOperation("注销账户")
    public ResultVO<?> closeUser() {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        System.out.println(username);
        if (username != null){
            Long id = userService.getIdByUsername(username);
            boolean successOrError = userService.removeAllById(id);
            if (successOrError){ return ResultVO.success("注销成功"); }
            else { return ResultVO.error("注销失败"); }
        }
        return ResultVO.error("注销异常，是否登录");
    }

}
